//
//  BaiduMapViewController.m
//  Cooking
//
//  Created by dllo on 15/10/13.
//  Copyright (c) 2015年 ZP.Y. All rights reserved.
//

#import "BaiduMapViewController.h"
#import <BaiduMapAPI/BMapKit.h>
#import "MyNaviButton.h"
#import "MyUrlButton.h"
#import "MyPointAnnotation.h"
#import "GMDCircleLoader.h"
#import "FoodlDetailViewController.h"
@interface BaiduMapViewController ()<BMKMapViewDelegate,BMKLocationServiceDelegate,BMKAnnotation,BMKPoiSearchDelegate,BMKRouteSearchDelegate,UITableViewDataSource,UITableViewDelegate,BMKShareURLSearchDelegate,UIWebViewDelegate,UISearchBarDelegate>

@property (nonatomic, assign)NSInteger a;
@property (nonatomic, retain)NSMutableArray *detailArr;
@property (nonatomic, retain)NSMutableArray *urlArr;
@property (nonatomic, retain)MyNaviButton *button1;
@property (nonatomic, retain)BMKMapView *mapView;
@property (nonatomic, retain)BMKLocationService *location;
@property (nonatomic, retain)BMKPointAnnotation *point;
@property (nonatomic, retain)BMKPointAnnotation *myPoint;
@property (nonatomic, retain)BMKPoiSearch *searchOne;
@property (nonatomic, retain)BMKPoiSearch *searchTwo;
@property (nonatomic, retain)BMKRouteSearch *route;
@property (nonatomic, retain)BMKWalkingRoutePlanOption *walkingRouteSearchOption;
@property (nonatomic, retain)BMKCircle* circle;
@property (nonatomic, retain)BMKNearbySearchOption *option;
@property (nonatomic, retain)UIView *mainView;
@property (nonatomic, retain)UIButton *rectButton;
@property (nonatomic, retain)UIButton *numButton;
@property (nonatomic, retain)UITableView *table;
@property (nonatomic, retain)UITableView *tableTwo;
@property (nonatomic, retain)NSMutableArray *arr;
@property (nonatomic, retain)BMKShareURLSearch *searcher;
@property (nonatomic, retain)BMKShareURLSearch *searcherTwo;
@property (nonatomic, assign)CLLocationCoordinate2D coor;
@property (nonatomic, retain)BMKNaviPara *navi;
@property (nonatomic, retain)MyUrlButton *button2;
@property (nonatomic, retain)UIWebView *web;
@property (nonatomic, retain)UIView *webView;
@property (nonatomic, retain)UIView *searchView;
@property (nonatomic, retain)UISearchBar *searchBar;
@property (nonatomic, retain)UITableView *searchtable;
@property (nonatomic, retain)NSMutableArray *searchArrOne;
@property (nonatomic, retain)NSMutableArray *searchArrTwo;
@property (nonatomic, assign)BOOL isSearch;

@end

@implementation BaiduMapViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arr = [NSMutableArray array];
        self.urlArr = [NSMutableArray array];
        self.detailArr = [NSMutableArray array];
        self.searchArrOne = [NSMutableArray array];
        self.searchArrTwo = [NSMutableArray array];
        self.a = 0;
        self.navigationItem.title = @"地图搜索";
        
    }
    return self;
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [self.mapView viewWillAppear];
    self.mapView.delegate = self;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    [self.mapView viewWillDisappear];
    self.mapView.delegate = nil;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self createView];
    [self createMapView];
    [self createLocationService];
}

- (void)createMapView
{
    self.mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 46, self.view.frame.size.width, self.view.frame.size.height -46)];
    [self.mapView showsUserLocation];
    
    [self.view addSubview:self.mapView];
    
}
- (void)createView
{
    self.mainView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 46)];
//    _mainView.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.8 alpha:0.9];
    _mainView.backgroundColor = [UIColor cyanColor];
    _mainView.alpha = 0.5;
//    _mainView.alpha = 0.9;
    [self.view addSubview:_mainView];
    
    
    self.rectButton = [[UIButton alloc]initWithFrame:CGRectMake(5, 5, self.view.frame.size.width / 3 - 30, 36)];
    [_rectButton setTitle:@"定位范围选择" forState:UIControlStateNormal];
    [_rectButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _rectButton.layer.masksToBounds = YES;
    _rectButton.layer.cornerRadius = 10;
    _rectButton.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    [_rectButton addTarget:self action:@selector(selectRect:) forControlEvents:UIControlEventTouchUpInside];
    self.rectButton.backgroundColor = [UIColor whiteColor];
    self.rectButton.alpha = 1;
    [self.mainView addSubview:self.rectButton];
    
    self.numButton = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 3 - 20, 5, self.view.frame.size.width /3 - 30, 36)];
    [self.numButton setTitle:@"结果显示数量" forState:UIControlStateNormal];
    [self.numButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _numButton.layer.masksToBounds = YES;
    _numButton.layer.cornerRadius = 10;
    _numButton.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    [_numButton addTarget:self action:@selector(selectNum:) forControlEvents:UIControlEventTouchUpInside];
    self.numButton.backgroundColor = [UIColor whiteColor];
    [self.mainView addSubview:self.numButton];
    
    
    self.searchView = [[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width * 2 / 3 - 40, 5, self.view.frame.size.width / 3 + 30, 36)];
    self.searchView.backgroundColor = [ UIColor whiteColor];
    self.searchView.layer.masksToBounds = YES;
    self.searchView.layer.cornerRadius = 10;
    [self.mainView addSubview:self.searchView];
    
    UIImageView *imageV = [[ UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width /3  , 6, 22, 22)];
    imageV.image = [UIImage imageNamed:@"search.png"];
    [self.searchView addSubview:imageV];
    
    UILabel *searchLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 0, self.view.frame.size.width / 3 -10, 36)];
    searchLabel.text = @"点击搜索美食";
    searchLabel.textAlignment = 1;
    searchLabel.textColor = [UIColor lightGrayColor];
    searchLabel.font = [UIFont boldSystemFontOfSize: 15];
    [self.searchView addSubview:searchLabel];
    
    UITapGestureRecognizer *searchTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(searchTap:)];
    [self.searchView addGestureRecognizer:searchTap];
    
}
- (void)searchTap:(UITapGestureRecognizer *)tap
{
    self.navigationController.navigationBarHidden = YES;
    [self.mainView removeFromSuperview];
    self.mapView.frame = CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64);
    
    self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
    self.searchBar.showsCancelButton = YES;
    self.searchBar.delegate = self;
    [self.view addSubview:self.searchBar];
    
    self.searchtable  = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64)];
    self.searchtable.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
    self.searchtable.tag = 35;
    self.searchtable.delegate = self;
    self.searchtable.dataSource = self;
    self.searchtable.rowHeight = 80;
    [self.view addSubview:self.searchtable];
    [self.searchBar becomeFirstResponder];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    self.navigationController.navigationBarHidden = NO;
    self.isSearch = NO;
    [self.searchtable reloadData];
    [_searchtable removeFromSuperview];
    [_searchBar removeFromSuperview];
    [self createView];
    self.mapView.frame = CGRectMake(0, 46, self.view.frame.size.width, self.view.frame.size.height - 46);
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    [self filterBySubstring:searchText];
}
- (void)filterBySubstring:(NSString *)subStr
{
    self.searchArrTwo = [NSMutableArray arrayWithArray:self.searchArrOne];
    self.isSearch = YES;
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@",subStr];
    self.searchArrTwo = (NSMutableArray *)[self.searchArrOne filteredArrayUsingPredicate:pred];
    [self.searchtable reloadData];
    
}
- (void)selectNum:(id)sender
{
    if (self.tableTwo.frame.size.height == 0) {
        
        self.tableTwo = [[UITableView alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 3 - 20, 46, self.view.frame.size.width/3 - 30, 0) style:UITableViewStylePlain];
        
        [UIView animateWithDuration:0.5 animations:^{
            _tableTwo.frame = CGRectMake(self.view.frame.size.width / 3 - 20, 46, self.view.frame.size.width/3 - 30, 200);
        }];
        _tableTwo.tag = 15;
        _tableTwo.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableTwo.delegate = self;
        _tableTwo.rowHeight = 40;
          _tableTwo.alpha = 0.7;
        _tableTwo.dataSource = self;
        [self.view addSubview:_tableTwo];
    }else{
        [UIView animateWithDuration:0.5 animations:^{
            _tableTwo.frame = CGRectMake(self.view.frame.size.width/ 3 - 20, 46, self.view.frame.size.width/3 - 30, 0);
        }];
    }
    
}
- (void)selectRect:(id)sender
{
    if (self.table.frame.size.height == 0) {
        
        self.table = [[UITableView alloc]initWithFrame:CGRectMake(5, 46, self.view.frame.size.width/3 - 30, 0) style:UITableViewStylePlain];
        
        [UIView animateWithDuration:0.5 animations:^{
            _table.frame = CGRectMake(5, 46, self.view.frame.size.width/3 - 30, 200);
        }];
        _table.tag = 10;
        _table.separatorStyle = UITableViewCellSeparatorStyleNone;
        _table.alpha = 0.7;
        _table.delegate = self;
        _table.rowHeight = 30;
        _table.dataSource = self;
        [self.view addSubview:_table];
    }else{
        [UIView animateWithDuration:0.5 animations:^{
            _table.frame = CGRectMake(5, 46, self.view.frame.size.width/3 - 30, 0);
        }];
    }
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView.tag == 10) {
        
        
        return 10;
    }
    if (tableView.tag == 15) {
        return 5;
    }
    if (tableView.tag == 35) {
        if (self.isSearch) {
            return self.searchArrTwo.count;
        }else{
            return self.searchArrOne.count;
        }
    }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 10) {
        
        
        static NSString *cellIdentify = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify];
        }
        cell.textLabel.text = [NSString stringWithFormat:@"%ld",(indexPath.row + 1) *500];
        cell.textLabel.textAlignment = 1;
        return cell;
    }
    if (tableView.tag == 15) {
        static NSString *cellIdentify = @"cell1";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify];
        }
        cell.textLabel.text = [NSString stringWithFormat:@"%ld",(indexPath.row + 1) *10];
        cell.textLabel.textAlignment = 1;
        return cell;
        
    }
    if (tableView.tag == 35) {
        NSString *cellIdentify = @"cell3";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
        if (!cell) {
            cell = [[ UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify];
        }
        if (self.isSearch) {
            cell.textLabel.textColor = [UIColor whiteColor];
            NSString *str = [self.searchArrTwo objectAtIndex:indexPath.row];
            NSMutableAttributedString *progressStr = [[NSMutableAttributedString alloc] initWithString:str];
            [progressStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:[str rangeOfString:self.searchBar.text]];
            [progressStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:[str rangeOfString:self.searchBar.text]];
            
            cell.textLabel.attributedText = progressStr;
            cell.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
            
        }else
        {
            cell.textLabel.textColor = [UIColor whiteColor];
            cell.textLabel.text = [self.searchArrOne objectAtIndex:indexPath.row];
            cell.backgroundColor =  [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
        }
        return cell;
    }
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 10) {
        
        
        self.table.frame = CGRectMake(0, 46, self.view.frame.size.width/3, 0);
        [self.rectButton setTitle:[NSString stringWithFormat:@"%ld",(indexPath.row + 1) *500] forState:UIControlStateNormal];
        self.option.radius = (int)(indexPath.row + 1) *500;
        self.circle.radius = (indexPath.row + 1) *500;
        [_searchOne poiSearchNearBy:_option];
        
    }
    if (tableView.tag == 15) {
        _tableTwo.frame = CGRectMake(self.view.frame.size.width/ 3, 110, self.view.frame.size.width/3, 0);
        [self.numButton setTitle:[NSString stringWithFormat:@"%ld",(indexPath.row + 1) *10] forState:UIControlStateNormal];
        self.option.pageCapacity = (int)(indexPath.row + 1) *10;
        [_searchOne poiSearchNearBy:_option];
    }
    if (tableView.tag == 35) {
        FoodlDetailViewController *med =[[FoodlDetailViewController alloc]init];
        self.navigationController.navigationBarHidden = NO;
        if (self.isSearch) {
            
            NSString *str =   [self.searchArrTwo objectAtIndex:indexPath.row];
            med.title = str;
            for (NSDictionary *dic  in self.urlArr) {
                NSString *strUrl = [[dic allKeys]lastObject];
                if ([str isEqualToString:strUrl]) {
                    med.url = [dic objectForKey:str];
                }
            }
            [self.navigationController pushViewController:med animated:YES];
        }else{
            NSString *str =  [self.searchArrOne objectAtIndex:indexPath.row];
            med.title = str;
            for (NSDictionary *dic  in self.urlArr) {
                NSString *strUrl = [[dic allKeys]lastObject];
                if ([str isEqualToString:strUrl]) {
                    med.url = [dic objectForKey:str];
                }
            }
            [self.navigationController pushViewController:med animated:YES];
        }
    }
    
}
- (void)createLocationService
{
    self.location = [[BMKLocationService alloc]init];
    _location.delegate = self;
    [_location startUserLocationService];
    
    
    self.searchOne = [[BMKPoiSearch alloc]init];
    self.searchOne.delegate = self;
    
}
//- (void)onGetWalkingRouteResult:(BMKRouteSearch*)searcher result:(BMKWalkingRouteResult*)result errorCode:(BMKSearchErrorCode)error
//{
//    for (BMKWalkingRouteLine *line in result.routes) {
//        for (BMKWalkingStep *step in line.steps) {
//
//        }
//    }
//}
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    self.coor = userLocation.location.coordinate;
    
    if (self.myPoint == nil) {
        
        
        self.mapView.centerCoordinate= userLocation.location.coordinate;
        
        
        self.mapView.zoomLevel = 17;
        self.myPoint = [[BMKPointAnnotation alloc]init];
        self.myPoint.coordinate = userLocation.location.coordinate;
        self.myPoint.title = @"当前位置";
        
        [self.mapView addAnnotation:self.myPoint];
        
        [self search:userLocation.location.coordinate];
        
        CLLocationCoordinate2D coor;
        coor = userLocation.location.coordinate;
        self.circle = [BMKCircle circleWithCenterCoordinate:coor radius:500];
        
        
        [_mapView addOverlay:self.circle];
    }else{
        self.myPoint.coordinate = userLocation.location.coordinate;
    }
    
    
    
}
- (void)search:(CLLocationCoordinate2D)coor
{
    
    //发起检索
    self.option = [[BMKNearbySearchOption alloc]init];
    _option.radius = 500;
    _option.pageCapacity = 10;
    _option.location = coor;
    //搜索关键字
    _option.keyword = @"美食";
    BOOL flag = [_searchOne poiSearchNearBy:_option];
    if(flag)
    {
        NSLog(@"周边检索发送成功");
    }
    else
    {
        NSLog(@"周边检索发送失败");
    }
    
}
- (void)onGetPoiDetailShareURLResult:(BMKShareURLSearch *)searcher result:(BMKShareURLResult *)result errorCode:(BMKSearchErrorCode)error
{
    if (error == BMK_SEARCH_NO_ERROR) {
        //在此处理正常结果
    }
    else {
        NSLog(@"抱歉，未找到结果2");
        
    }
}
//处理位置信息你分享了URL结果
//- (void)onGetLocationShareURLResult:(BMKShareURLSearch *)searcher result:(BMKShareURLResult *)result errorCode:(BMKSearchErrorCode)error
//{
//    if (error == BMK_SEARCH_NO_ERROR) {
////        在此处理正常结果
//    }
//    else {
//        NSLog(@"抱歉，未找到结果");
//    }
//}

- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay{
    if ([overlay isKindOfClass:[BMKCircle class]]){
        
        BMKCircleView* circleView = [[BMKCircleView alloc] initWithOverlay:overlay];
        circleView.fillColor = [[UIColor blueColor] colorWithAlphaComponent:0.2];
        circleView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.2];
        circleView.lineWidth = 1.0;
        
        return circleView;
    }
    return nil;
}
- (void)onGetPoiResult:(BMKPoiSearch*)searcher result:(BMKPoiResult*)poiResultList errorCode:(BMKSearchErrorCode)error
{
    
    if (error == BMK_SEARCH_NO_ERROR) {
        [self.mapView removeAnnotations:self.arr];
        [self.arr removeAllObjects];
        
        //        self.route = [[BMKRouteSearch alloc] init];
        //        self.route.delegate = self;
        //        BMKPlanNode *start = [[BMKPlanNode alloc] init];
        //        BMKPoiInfo * info1 = [poiResultList.poiInfoList objectAtIndex:0];
        //        start.pt = info1.pt;
        //        BMKPoiInfo * info2 = [poiResultList.poiInfoList objectAtIndex:1];
        //        BMKPlanNode *end = [[BMKPlanNode alloc] init];
        //        end.pt = info2.pt;
        //
        //        self.walkingRouteSearchOption = [[BMKWalkingRoutePlanOption alloc]init];
        //        _walkingRouteSearchOption.from = start;
        //        _walkingRouteSearchOption.to = end;
        //
        //        BOOL flag = [_route walkingSearch:_walkingRouteSearchOption];
        //        if (flag) {
        //            NSLog(@"搜索路线成功");
        //        }else{
        //            NSLog(@"搜索路线失败");
        //        }
        
        
        for (BMKPoiInfo *info in poiResultList.poiInfoList) {
            NSLog(@"周边+++%@",info.name);
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            //            [dic setObject:[NSNumber numberWithDouble:info.pt.latitude] forKey:[NSString stringWithFormat:@"%@.%@",info.name,@"latitude"]];
            //            [dic setObject:[NSNumber numberWithDouble:info.pt.longitude] forKey:[NSString stringWithFormat:@"%@.%@",info.name,@"longitude"]];
            
            [dic setObject:[NSString stringWithFormat:@"%lf",info.pt.latitude] forKey:[NSString stringWithFormat:@"%@.%@",info.name,@"latitude"]];
            [dic setObject:[NSString stringWithFormat:@"%lf",info.pt.longitude] forKey:[NSString stringWithFormat:@"%@.%@",info.name,@"longitude"]];
            
            [self.detailArr addObject:dic];
            _point.coordinate = info.pt;
            self.point = [[BMKPointAnnotation alloc]init];
            _point.subtitle = info.address;
            _point.title = info.name;
            [self.mapView addAnnotation:_point];
            [self.arr addObject:_point];
            //            self.button1.coordinate = info.pt;
            
            //初始化检索服务
            self.searchTwo = [[BMKPoiSearch alloc] init];
            _searchTwo.delegate = self;
            
            //POI详情检索
            BMKPoiDetailSearchOption* option = [[BMKPoiDetailSearchOption alloc] init];
            option.poiUid = info.uid;//POI搜索结果中获取的uid
            
            BOOL flag = [_searchTwo poiDetailSearch:option];
            if(flag)
            {
                //详情检索发起成功
            }
            else
            {
                //详情检索发送失败
            }
            
            _searcher =[[BMKShareURLSearch alloc]init];
            _searcher.delegate = self;
            
            
            //发起短串搜索获取poi分享url
            BMKPoiDetailShareURLOption *detailShareUrlSearchOption = [[BMKPoiDetailShareURLOption alloc]init];
            //从poi检索得到的poi的uid
            detailShareUrlSearchOption.uid=info.uid;
            BOOL flag1 = [_searcher requestPoiDetailShareURL:detailShareUrlSearchOption];
            
            if(flag1)
            {
                NSLog(@"详情url检索发送成功");
            }
            else
            {
                NSLog(@"详情url检索发送失败");
            }
            //发起位置信息分享URL检索
            //            BMKLocationShareURLOption *option = [[BMKLocationShareURLOption alloc]init];
            //            option.snippet = info.address;
            //            option.name = info.name;
            //            option.location = info.pt;
            //            BOOL flag1 = [_searcher requestLocationShareURL:option];
            //
            //            if(flag1)
            //            {
            //                NSLog(@"位置信息分享URL检索发送成功");
            //            }
            //            else
            //            {
            //                NSLog(@"位置信息分享URL发送失败");
            //            }
            //
        }
        
        
    }
    else if (error == BMK_SEARCH_AMBIGUOUS_KEYWORD){
        //当在设置城市未找到结果，但在其他城市找到结果时，回调建议检索城市列表
        // result.cityList;
        NSLog(@"起始点有歧义");
    } else {
        NSLog(@"抱歉，未找到结果1");
        
        [self search:self.coor];
    }
}
//第二步，设置结果监听：
-(void)onGetPoiDetailResult:(BMKPoiSearch *)searcher result:(BMKPoiDetailResult *)poiDetailResult errorCode:(BMKSearchErrorCode)errorCode
{
    if(errorCode == BMK_SEARCH_NO_ERROR){
        //在此处理正常结果
        NSMutableDictionary *dic  = [NSMutableDictionary dictionary];
        [dic setObject:poiDetailResult.detailUrl forKey:poiDetailResult.name];
        [self.urlArr addObject:dic];
        
        //        NSLog(@"%@----%lf------%@-------%@",poiDetailResult.name,poiDetailResult.overallRating,poiDetailResult.tag,poiDetailResult.detailUrl);
        
        [self.web reload];
    }
    
    NSLog(@"%@",self.urlArr);
    for (NSDictionary *dic in self.urlArr) {
        if ([self.searchArrOne containsObject:[[dic allKeys]lastObject]]) {
            
        }else{
            [self.searchArrOne addObjectsFromArray: [dic allKeys]];}
    }
    
    
    
}
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation
{
    BMKPointAnnotation * point = (BMKPointAnnotation *)annotation;
    
    if (point == self.point) {
        
        BMKAnnotationView *ann = [[BMKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"ann"];
        
        ann.annotation = annotation;
        
        CLLocationCoordinate2D cool;
        
        for (NSMutableDictionary *dic in self.detailArr) {
            NSArray *arr = [dic allKeys];
            for (NSString *key in arr) {
                if ([[NSString stringWithFormat:@"%@.%@",point.title,@"latitude"]isEqualToString:key]) {
                    cool.latitude = [[dic objectForKey:key]doubleValue];
                    NSLog(@"1");
                }
                if ([[NSString stringWithFormat:@"%@.%@",point.title,@"longitude"]isEqualToString:key]) {
                    cool.longitude = [[dic objectForKey:key]doubleValue];
                    NSLog(@"2");
                }
            }
        }
        //    [ann.annotation setCoordinate:mapView.centerCoordinate];
        MyUrlButton * button2 = [[MyUrlButton alloc]initWithFrame:CGRectMake(0, 0, 32, 41)];
        button2.tag = self.a;
        button2.title = point.title;
        [button2 setTitle:@"点击详情" forState:UIControlStateNormal];
        [button2 setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        button2.titleLabel.font = [UIFont boldSystemFontOfSize:14];
        [button2 addTarget:self action:@selector(addView:) forControlEvents:UIControlEventTouchUpInside];
        button2.titleLabel.numberOfLines = 0;
        
//        [button2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        
        self.a ++;
        
        
        
        MyNaviButton* button1 = [[MyNaviButton alloc]initWithFrame:CGRectMake(0, 0, 32, 41)];
        [button1 setTitle:@"导航" forState:UIControlStateNormal];
        button1.titleLabel.font = [UIFont boldSystemFontOfSize:14];
        button1.titleLabel.numberOfLines = 0;
        [button1 addTarget:self action:@selector(navi:) forControlEvents:UIControlEventTouchUpInside cll:cool];
        [button1 setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        button1.coordinate = cool;
        button1.title = point.title;
        ann.image = [UIImage imageNamed:@"store.png"];
        ann.rightCalloutAccessoryView = button1;
        ann.leftCalloutAccessoryView = button2;
        return ann;
    }else{
        BMKAnnotationView *ann2 = [[BMKAnnotationView alloc]initWithAnnotation:self reuseIdentifier:@"ann2"];
        ann2.annotation = annotation;
        ann2.image = [UIImage imageNamed:@"my.png"];
        return ann2;
    }
}
- (void)navi:(id)sender
{
    MyNaviButton *button  = (MyNaviButton *)sender;
    
    
    //    NSLog(@"%f",button.coordinate.latitude);
    BMKPlanNode *start = [[BMKPlanNode alloc]init];
    start.pt = self.coor;
    BMKPlanNode *end = [[BMKPlanNode alloc]init];
    end.pt = button.coordinate;
    self.navi = [[BMKNaviPara alloc]init];
    _navi.startPoint = start;
    _navi.isSupportWeb = YES;
    _navi.appName = nil;
    _navi.endPoint = end;
    NSLog(@"%@",button.title);
    [BMKNavigation openBaiduMapNavigation:_navi];
    
}
- (void)addView:(id)sender
{
    
    MyUrlButton *button = (MyUrlButton *)sender;
    NSLog(@"----%@",button.title);
    NSInteger b = button.tag;
    NSLog(@"%ld",b);
    self.webView = [[UIView alloc]initWithFrame:CGRectMake(30, 140, self.view.frame.size.width - 60, self.view.frame.size.height - 170)];
    self.webView.layer.masksToBounds = YES;
    self.webView.layer.cornerRadius = 10;
    self.webView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.8];
    
    //    self.view.userInteractionEnabled = NO;
    
    [self.view addSubview:self.webView];
    NSString *str = @"";
    for (NSDictionary *dic in self.urlArr) {
        NSArray *arr = [dic allKeys];
        NSString *strKey = [arr lastObject];
        if ([button.title isEqualToString:strKey]) {
            str = [dic objectForKey:button.title];
        }
    }
    NSLog(@"!!!%@",str);
    self.web = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.webView.frame.size.width, self.webView.frame.size.height)];
    NSURL *url = [NSURL URLWithString:str];
    self.web.scalesPageToFit = YES;
    self.web.userInteractionEnabled = YES;
    self.web.delegate = self;
    
    //    view.userInteractionEnabled = YES;
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [_web loadRequest:request];
    [self.webView addSubview:_web];
    
    if (str == nil || [str isEqualToString:@""] || [str isEqualToString:@"http://map.baidu.com/"]) {
        [self.webView removeFromSuperview];
        [_web removeFromSuperview];
        self.view.userInteractionEnabled = YES;
        
        
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"暂未查找到详情" delegate:self cancelButtonTitle:@"继续搜索" otherButtonTitles: nil];
        [alert show];
        
    }else{
        
        
        [GMDCircleLoader setOnView:self.view withTitle:@"正在努力加载中..." animated:YES];
    }
    
    UITapGestureRecognizer *tapBack = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapBack:)];
    [self.mapView addGestureRecognizer:tapBack];
    
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [GMDCircleLoader hideFromView:self.view animated:YES];
}
- (void)tapBack:(UITapGestureRecognizer *)tap
{
    [_web removeFromSuperview];
    [self.webView removeFromSuperview];
    [self.mapView removeGestureRecognizer:tap];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}





@end
